import 'package:flutter/material.dart';
import 'package:qunaimedical/pages/web_page.dart';
import 'package:qunaimedical/utils/auth_util.dart';
import 'package:qunaimedical/utils/dialog_util.dart';
import 'package:qunaimedical/values/configs.dart';
import 'package:qunaimedical/values/navigation.dart';
import 'package:qunaimedical/values/urls.dart';

void navigateWithRoute(BuildContext context, Navigation navigation,
    {bool authStateRequired = false}) {
  if (authStateRequired) {
    if (AuthUtil.isLogin()) {
      Navigator.pushNamed(context, navigation.name);
    } else {
      DialogUtil.showAlertDialog(context, '您需要登录后才可以使用该功能。', onPressed: () {
        Navigator.pop(context);
        Navigator.pushNamed(context, Navigation.login.name);
      });
    }
  } else {
    Navigator.pushNamed(context, navigation.name);
  }
}

void navigateTo(BuildContext context, Widget widget,
    {bool authStateRequired = false}) {
  if (authStateRequired) {
    if (AuthUtil.isLogin()) {
      Navigator.push(context, MaterialPageRoute(builder: (c) => widget));
    } else {
      DialogUtil.showAlertDialog(context, '您需要登录后才可以使用该功能。', onPressed: () {
        Navigator.pop(context);
        Navigator.pushNamed(context, Navigation.login.name);
      });
    }
  } else {
    Navigator.push(context, MaterialPageRoute(builder: (c) => widget));
  }
}

void toMyConsultation(BuildContext context, MyConsultationType type) async {
  String url = "${Urls.CONSULTATIONS}?handle=${type.name}";
  navigateTo(
    context,
    WebPage(
      url: url,
      title: "我的会诊",
    ),
    authStateRequired: true,
  );
}
